/*
 * ad_demo.cpp
 * This file is part of dynad
 *
 * Copyright (C) 2014 - Sebastian Gesemann
 *
 * dynad is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * dynad is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with dynad. If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include <iostream>
 #include <cmath>
 #include "ad.hpp"

 using namespace std;

 int main()
 {
     advar x = adiff(2,'x'); // We set our variable to 2. The 'x' is just an ID for
                             // the variable whose derivative should be tracked. The
                             // ID is actually an integer (not limited to characters).

     advar y = x*sin(3/x);   // some possibly complicated mathematical expression

     cout << value_of(y) << endl;            // output the value of y
     cout << derivs_of(y).get('x') << endl;  // output derivative of y with respect to x

     // derivs_of for advar yields something like a map we can iterate over.
     // This should be useful if more than one variable is involved.
     for (auto const& p : derivs_of(y)) {
         // p refers to a std::pair<int,double>
         cout << char(p.first) << ": " << p.second << endl;
     }

     // Note: The result of derivs_of for expressions not of type advar
     // does NOT provide add/get/begin/end functions! The derivatives
     // would have to be "gathered" into a map first (such as the one
     // that is part of an advar object).
 }

